/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.accident.controller;

import com.koron.accident.domain.PeccancyRecord;
import com.koron.accident.service.api.PeccancyRecordService;
import com.koron.accident.util.BusinessCode;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

@Api(tags = "peccancyRecord",description = "事故/事件-违章记录相关接口类")
@RestController
@RequestMapping("/peccancyRecord")
public class PeccancyRecordController {

    @Autowired
    private PeccancyRecordService recordService;

    @ApiOperation("保存事件管理接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "违章记录id,新增不传，修改必传",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "corpCode",value = "水司code",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "peccancyStaff",value = "违章人员code",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "peccancyStaffName",value = "违章人员名称",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "peccancyStaffDept",value = "违章人员所在部门code",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "peccancyStaffDeptName",value = "违章人员所在部门名称",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "peccancyBehaviour",value = "违章行为",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "peccancyDegree",value = "违章程度0一般，1严重2恶性",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "peccancyType",value = "违章类型，0事件管理1事故报告",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "businessId",value = "事件管理/事故报告id",required = true,dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/save.htm",method = RequestMethod.POST)
    public MessageBean<Integer> save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,@ApiIgnore PeccancyRecord record){
        if (null==user){
            MessageBean<Integer> messageBean=new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LONGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return messageBean;
        }

        record.setUpdateUser(user.getCode());
        record.setUpdateUserName(user.getName());
        record.setUpdateTime(new Date());
        return recordService.save(record);
    }

    @ApiOperation("删除违章记录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "违章记录id",required = true,dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/delete.htm",method = RequestMethod.POST)
    public MessageBean<Integer> delete(@RequestParam final String id){

        return recordService.delete(id);
    }

    @ApiOperation("查询单个违章记录详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "违章记录id",required = true,dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/details.htm",method = RequestMethod.POST)
    public MessageBean<PeccancyRecord> details(@RequestParam final String id){

        return recordService.queryOne(id);
    }

    @ApiOperation("违章记录列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId",value = "事件管理/事故报告id",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,dataType = "int",paramType = "query"),
    })
    @RequestMapping(value = "/list.htm",method = RequestMethod.POST)
    public MessageBean<DataBean<PeccancyRecord>> list(@RequestParam final String businessId, @RequestParam Integer page, @RequestParam Integer pageSize){
        Integer start=null;
        if (page==null||page<=0){
            page=1;
            if (pageSize==null||pageSize<=0){
                pageSize=15;
            }
        }
        start=(page<=1)?0:(page-1)*pageSize;
        MessageBean<DataBean<PeccancyRecord>> msg= recordService.list(businessId,start,pageSize);
        if (null!=msg.getData())
            msg.getData().setPageNo(page);
        return msg;
    }
}
